package com.edu.backend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.edu.backend.entity.PagerInfo;
import com.edu.backend.entity.User;
import com.edu.backend.service.UserService;
import com.edu.backend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/student")
   public R getStudentList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,User user){
        PagerInfo<User> page=new PagerInfo<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page=userService.getStudentList(page, user);
        String jsonStr = JSON.toJSONString(page, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }
    @GetMapping("/teacher")
    public R getTeacherList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,User user){
        PagerInfo<User> page=new PagerInfo<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page=userService.getTeacherList(page, user);
        String jsonStr = JSON.toJSONString(page, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }

    @DeleteMapping("/deleteStudent/{id}")
    public R deleteStudent(@PathVariable(value = "id") String id){
        Boolean result=userService.deleteStudent(id);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("/deleteTeacher/{id}")
    public R deleteTeacher(@PathVariable(value = "id") String id){
        Boolean result=userService.deleteTeacher(id);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @DeleteMapping("/deleteStudents")
    public R deleteBatchStudent(@RequestParam(value = "ids") String ids){

        String str[]=ids.split(",");
        List<String> list= Arrays.asList(str);
        Boolean result=userService.deleteBatch(list);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @DeleteMapping("/deleteTeachers")
    public R deleteBatchTeacher(@RequestParam(value = "ids") String ids){

        String str[]=ids.split(",");
        List<String> list= Arrays.asList(str);
        Boolean result=userService.deleteBatch(list);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/addStudent")
    public R addStudent(@RequestBody  User user){
        Boolean result=userService.addStudent(user);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody  User user){
        Boolean result=userService.addTeacher(user);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("/updateStudent")
    public R updateStudent(@RequestBody  User user){
        System.out.println(user.toString());
        Boolean result=userService.updateStudent(user);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @PutMapping("/updateTeacher")
    public R updateTeacher(@RequestBody  User user){
        System.out.println(user.toString());
        Boolean result=userService.updateStudent(user);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/getStudent/{id}")
    public R getStudent(@PathVariable(value = "id") String id){
        User user=userService.getStudent(Integer.valueOf(id));
        String jsonStr = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable(value = "id") String id){
        User user=userService.getStudent(Integer.valueOf(id));
        String jsonStr = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }
}
